package cn.com.wind.bdg.inmysql.inmysql.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.wind.bdg.inmysql.inmysql.entity.Info;
import cn.com.wind.bdg.inmysql.inmysql.utils.GetHTMLData;
import cn.com.wind.bdg.inmysql.inmysql.utils.GetHtml;
import cn.com.wind.bdg.inmysql.inmysql.utils.InsertDB;
import cn.com.wind.bdg.inmysql.inmysql.utils.JsoupGetCode;

@Service
public class Data2DataBase {
	
	@Autowired
	InsertDB insertDB;
	@Autowired
	GetHtml getHtml;
	private static Logger LOGGER = LoggerFactory.getLogger(Data2DataBase.class);
	/**
	 * 
	 * @Title:  Data2DataBase   
	 * @Description:    将数据存入info实体类，并存入mysql
	 * @param:  @throws Exception  
	 * @throws
	 */
	@SuppressWarnings("null")
	public void method() throws Exception {
		List<String> resUrl = new ArrayList<String>();
		GetHTMLData getHTMLData = new GetHTMLData();
		resUrl = getHtml.method();
		JsoupGetCode jsoupGetCode = new JsoupGetCode();
		//轮询detail页中的内容，并且存入mysql
		Info info = new Info();
		for(String url:resUrl) {
			info.setCOMPANY_NAME(getHTMLData.getCompanyName(url));
			info.setREGISTERED_ADDRESS(getHTMLData.getLocation(url));
			info.setPLANTING_VARIETIES(getHTMLData.getType(url));
			info.setPLANTING_DISTRICT(getHTMLData.getArea(url));
			info.setPLANTING_DISTRICT_CODE(jsoupGetCode.method(getHTMLData.getArea(url)));
			info.setREPORT_CODE(getHTMLData.getReportCode(url));
			try {
				insertDB.Insert(info);
			} catch (Exception e) {
				LOGGER.error("{}由于code后者公告号无值，存入MySQL失败",getHTMLData.getCompanyName(url), e);
			}
			
		}
	}
}
